/**
 * @Author: wangjian
 * 656876071@qq.com
 * @Date: 2024-09-04 10:04:27
 * @LastEditTime: 2024-09-05 11:21:58
 * @FilePath: src/core/common/hooks/DictHooks.ts
 * @Description: 字典辅助
 */

import {useDictApi} from "@/core/api/sys/DictUseApi";
import type {AxiosResponse} from "axios";
import type {Ret} from "@/core/utils/request";
export const DICT_COLLECTION_SYMBOL = Symbol("DICT_COLLECTION_SYMBOL")
/**
 * 字典数据包装
 */
interface DictCodeWrapper {
    [key: string]: any[]
}

export const useDict = (...dictCodes: string[]) => {
    const {listOptionsByCode} = useDictApi()
    return Promise.all(dictCodes.map(code => listOptionsByCode(code))).then((rets: AxiosResponse[] | Ret[]) => {
        const result: DictCodeWrapper = {}
        for (let i = 0; i < dictCodes.length; i++) {
            let ret = rets[i] as Ret
            if (ret.code == 0) {
                result[dictCodes[i]] = ret.data
            }else{
                console.error("字典加载失败", dictCodes[i], ret)
            }
        }
        return result
    })
}
